﻿using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

public class RegisterPanel : BasePanel
{
    public TMP_InputField username;
    public TMP_InputField password;
    public Button btnCancel;
    public Button btnSure;
   
    public override void Init()
    {
        btnCancel.onClick.AddListener(() =>
        {
            UIManager.Instance.HidePanel<RegisterPanel>();
            UIManager.Instance.ShowPanel<LoginPanel>();
        });
        btnSure.onClick.AddListener(() =>
        {
            if (username.text == "" || password.text == "")
            {
                UIManager.Instance.ShowPanel<TipPanel>().InitContent("请正确填写用户名或密码!");
                return;
            }
            if (LoginManager.Instance.Register(username.text, password.text))
            {
                TipPanel tipPanel = UIManager.Instance.ShowPanel<TipPanel>();
                tipPanel.InitContent("注册成功!");
                tipPanel.onRegister.AddListener(() =>
                {
                    UIManager.Instance.HidePanel<RegisterPanel>();
                    LoginData data = LoginManager.Instance.loginData;
                    data.username = username.text;
                    data.password = password.text;
                    data.isRemenberPwd = false;
                    data.isAutoLogin = false; 
                    data.serverId = 0;
                    LoginPanel loginPanel = UIManager.Instance.ShowPanel<LoginPanel>();
                });
            }
            else
            {
                UIManager.Instance.ShowPanel<TipPanel>().InitContent("用户名重复!");
            }
        });
    }
}
